package com.xh.pmo.actions;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.xh.pmo.base.ModelDrivenBaseAction;
import com.xh.pmo.domain.Bug;
import com.xh.pmo.domain.Project;
import com.xh.pmo.domain.Team;
import com.xh.pmo.domain.Team_User;
import com.xh.pmo.domain.User;
@Controller
@Scope("prototype")
@SuppressWarnings("serial")
public class BugAction extends ModelDrivenBaseAction<Bug>{

	
	
	private Long projectId;
	private File upload ;
	private Long userId;
	public String add () {
		
		Project project = projectService.getById(projectId);
		User handlePerson = userService.getById(userId);
		//bugService.findByProject(project);
		if(upload != null) {
			String path = upload(upload);
			
			model.setDocumentFile(path);
		}
		model.setVersion(project.getVerson());
		model.setCreatePerson(getCurrentUser());
		model.setHandlePerson(handlePerson);
		model.setCreateTime(new Date());
		model.setProject(project);
		
		project.setUnsolvedBug(project.getUnsolvedBug() + 1);
		bugService.save(model);
		
		
		return "add";
	}

	public String addUI () {
		
		Long start = System.currentTimeMillis();
		
		
		Project project = projectService.getById(projectId);
		Team team = project.getTeam();
		
		List<Team_User> tuList = team_userService.getTeamUserByTeam(team);
		ActionContext.getContext().put("tuList", tuList);
		ActionContext.getContext().put("project", project);
		
		Long end = System.currentTimeMillis();
		Long time = end - start;
		System.out.println("本次访问处理时间是  "  + time);
		
		return "addUI";
	}
	
	private String upload (File upload) {
		SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy_MM_DD_HH_mm_ss");
		String basePath  = ServletActionContext.getServletContext().getRealPath("/bugFile");
		File file = new File(basePath);
		if(!file.exists()) {
			file.mkdirs();
		}
		
		String fileName = simpleDate.format(new Date()) + UUID.randomUUID().toString();
		String path  = basePath + "/" + fileName;
		
		upload.renameTo(new File(path));
		return path;
	}
	public Long getProjectId() {
		return projectId;
	}


	public void setProjectId(Long projectId) {
		this.projectId = projectId;
	}


	public File getUpload() {
		return upload;
	}


	public void setUpload(File upload) {
		this.upload = upload;
	}


	public Long getUserId() {
		return userId;
	}


	public void setUserId(Long userId) {
		this.userId = userId;
	}
	
	
}
